package com.site.codegen.disposer.impl;

import com.alibaba.fastjson2.JSON;
import com.site.codegen.disposer.CommandDisposer;
import com.site.codegen.utils.DatabaseUtil;
import com.site.codegen.vo.Column;
import com.site.codegen.vo.DatasourceArgs;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.JDBCType;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ColumnListDisposer implements CommandDisposer {
    @Override
    public Object dispose(String data) {
        DatasourceArgs datasourceArgs = JSON.parseObject(data, DatasourceArgs.class);
        Connection connection = null;
        List<Column> list = new ArrayList<>();

        try {
            connection = DatabaseUtil.getConnection(datasourceArgs.getDriverClass(), datasourceArgs.getUrl(), datasourceArgs.getUsername(), datasourceArgs.getPassword(), datasourceArgs.getDriverPath());
            DatabaseMetaData databaseMetaData = connection.getMetaData();

            ResultSet columnResultSet = databaseMetaData.getColumns(datasourceArgs.getCatalog(), datasourceArgs.getSchema(), datasourceArgs.getTable(), null);
            while (columnResultSet.next()) {
                Column column = new Column();
                column.setTableCatalog(columnResultSet.getString("TABLE_CAT"));
                column.setTableSchema(columnResultSet.getString("TABLE_SCHEM"));
                column.setTableName(columnResultSet.getString("TABLE_NAME"));
                column.setColumnName(columnResultSet.getString("COLUMN_NAME"));
                column.setColumnSize(columnResultSet.getInt("COLUMN_SIZE"));
                column.setDecimalDigits(columnResultSet.getInt("DECIMAL_DIGITS"));
                column.setDataType(columnResultSet.getInt("DATA_TYPE"));
                column.setTypeName(columnResultSet.getString("TYPE_NAME"));
                column.setJdbcType(JDBCType.valueOf(columnResultSet.getInt("DATA_TYPE")).getName());
                column.setIsNullable(columnResultSet.getString("IS_NULLABLE"));
                column.setIsPrimaryKey("NO");
                column.setIsAutoincrement(columnResultSet.getString("IS_AUTOINCREMENT"));
                column.setColumnComment(columnResultSet.getString("REMARKS"));
//                System.out.println("TABLE_CAT ---->> " + columnResultSet.getString("TABLE_CAT"));
//                System.out.println("TABLE_SCHEM ---->> " + columnResultSet.getString("TABLE_SCHEM"));
//                System.out.println("TABLE_NAME ---->> " + columnResultSet.getString("TABLE_NAME"));
//                System.out.println("COLUMN_NAME ---->> " + columnResultSet.getString("COLUMN_NAME"));
//                System.out.println("DATA_TYPE ---->> " + columnResultSet.getString("DATA_TYPE"));
//                System.out.println("TYPE_NAME ---->> " + columnResultSet.getString("TYPE_NAME"));
//                System.out.println("COLUMN_SIZE ---->> " + columnResultSet.getString("COLUMN_SIZE"));
//                System.out.println("DECIMAL_DIGITS ---->> " + columnResultSet.getString("DECIMAL_DIGITS"));
//                System.out.println("NUM_PREC_RADIX ---->> " + columnResultSet.getString("NUM_PREC_RADIX"));
//                System.out.println("NULLABLE ---->> " + columnResultSet.getString("NULLABLE"));
//                System.out.println("REMARKS ---->> " + columnResultSet.getString("REMARKS"));
//                System.out.println("COLUMN_DEF ---->> " + columnResultSet.getString("COLUMN_DEF"));
//                System.out.println("SQL_DATA_TYPE ---->> " + columnResultSet.getString("SQL_DATA_TYPE"));
//                System.out.println("SQL_DATETIME_SUB ---->> " + columnResultSet.getString("SQL_DATETIME_SUB"));
//                System.out.println("CHAR_OCTET_LENGTH ---->> " + columnResultSet.getString("CHAR_OCTET_LENGTH"));
//                System.out.println("ORDINAL_POSITION ---->> " + columnResultSet.getString("ORDINAL_POSITION"));
//                System.out.println("IS_NULLABLE ---->> " + columnResultSet.getString("IS_NULLABLE"));
//                System.out.println("SCOPE_CATALOG ---->> " + columnResultSet.getString("SCOPE_CATALOG"));
//                System.out.println("SCOPE_SCHEMA ---->> " + columnResultSet.getString("SCOPE_SCHEMA"));
//                System.out.println("SCOPE_TABLE ---->> " + columnResultSet.getString("SCOPE_TABLE"));
//                System.out.println("SOURCE_DATA_TYPE ---->> " + columnResultSet.getString("SOURCE_DATA_TYPE"));
//                System.out.println("IS_AUTOINCREMENT ---->> " + columnResultSet.getString("IS_AUTOINCREMENT"));
//                System.out.println("IS_GENERATEDCOLUMN ---->>" + columnResultSet.getString("IS_GENERATEDCOLUMN"));
                list.add(column);
            }
            columnResultSet.close();

            ResultSet primaryKeyResultSet = databaseMetaData.getPrimaryKeys(datasourceArgs.getCatalog(), datasourceArgs.getSchema(), datasourceArgs.getTable());
            while (primaryKeyResultSet.next()) {
                String primaryKeyColumnName = primaryKeyResultSet.getString("COLUMN_NAME");
//                System.out.println("COLUMN_NAME ---->> " + primaryKeyResultSet.getString("COLUMN_NAME"));
//                System.out.println("PK_NAME ---->>" + primaryKeyResultSet.getString("PK_NAME"));
                list.stream().filter(item -> primaryKeyColumnName.equals(item.getColumnName())).findFirst().ifPresent(item -> item.setIsPrimaryKey("YES"));
            }
            primaryKeyResultSet.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return list;
    }
}
